https://leetcode.cn/problems/surrounded-regions/
class Solution {
    typedef pair<int,int> PII;
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};

    int m,n;
public:
    void solve(vector<vector<char>>& board) {
        m=board.size(),n=board[0].size();
        //处理边界
        for(int i=0;i<n;i++)
        {
            if(board[0][i]=='O')    bfs(board,0,i);
            if(board[m-1][i]=='O')  bfs(board,m-1,i);
        }

        for(int j=0;j<m;j++)
        {
            if(board[j][0]=='O')    bfs(board,j,0);
            if(board[j][n-1]=='O')    bfs(board,j,n-1);
        }

        //还原并处理非边界
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(board[i][j]=='O')  board[i][j]='X';
                else if(board[i][j]=='*') board[i][j]='O';
            }
        }
        
    }

    void bfs(vector<vector<char>>& board,int sr,int sc)
    {
        queue<PII>q;
        q.push({sr,sc});
        board[sr][sc]='*';
        while(!q.empty())
        {
            auto [a,b]=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                int x=a+dx[i],y=b+dy[i];
                if(x>=0&&x<m&& y>=0&&y<n && board[x][y]=='O')
                {
                    board[x][y]='*';
                    q.push({x,y});
                }
            }
        }
    }
};